<template>
  <el-row>
    <edit-modal ref="editModal" @refresh="refresh"></edit-modal>
    <el-col :span="24">
      <el-card class="search-form">
        <el-form ref="searchForm" class="el-form-col el-form-s" :model="search" label-position="left" label-width="80px">
          <el-row :gutter="15">
            <el-col :span="6">
              <el-form-item prop="logAccount" label="登录账号:">
                <el-input v-model="search.logAccount" placeholder="" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="师傅姓名:" prop="workerName">
                <el-input v-model="search.workerName" placeholder=""></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="联系手机:" prop="phone">
                <el-input v-model="search.phone" placeholder=""></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="2" class="text-center">
              <el-button type="success" :loading="searchLoading" plain @click="searchFunc(search)">搜索</el-button>
            </el-col>
            <el-col :span="2" class="text-center">
              <el-button type="primary" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>
            </el-col>
            <el-col :span="2" class="text-center">
              <el-button @click="resetFields" plain>重置</el-button>
            </el-col>
          </el-row>

          <el-collapse-transition>
            <template  v-if="isOpen">
              <el-row :gutter="15">
                <el-col :span="6">
                  <el-form-item label="业务类型:" prop="businessNo">
                    <el-select v-model="search.businessNo" placeholder="请选择">
                      <el-option v-for="item in businessArr"
                                 :key="item.value"
                                 :label="item.text"
                                 :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="服务类型:" prop="serviceNo">
                    <el-select v-model="search.serviceNo" placeholder="请选择">
                      <el-option v-for="item in serviceArr"
                                 :key="item.value"
                                 :label="item.text"
                                 :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="服务区域:" prop="addressArr">
                    <CascadeAddress v-model="search.addressArr" :level="3" changeOnSelect />
                  </el-form-item>
                </el-col>
              </el-row>
            </template>
          </el-collapse-transition>
        </el-form>
      </el-card>
    </el-col>
    <el-col :span="24">
      <BasicTable
        ref="BasicTable"
        url="/ms-warehouse-order/resourceManage/selectInstalls"
        :tabs="tabs"
        :columns="columns">
        <template slot="buttons">
          <el-button type="primary" @click="add">新增</el-button>
        </template>
        <template slot="column">
          <el-table-column fixed="right" width="100px" align="center" label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="editRowId(scope.row)"
              >
                修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="delRowId(scope.row)"
              >
                移除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </BasicTable>
    </el-col>
  </el-row>
</template>
<script>
import EditModal from '@/views/oms/resource/install/EditModal'
import {copy, dotData, confirm} from '@/utils'
import {getInstallBusinessType, getInstallServiceType} from '@/utils/selectOptions'
export default {
  name: 'resourceInstall',
  components: {
    EditModal
  },
  data() {
    let columns = [
      {
        label: '登录账号',
        data: 'logAccount'
      },
      {
        label: '师傅姓名',
        data: 'workerName'
      },
      {
        label: '联系手机',
        data: 'phone'
      },
      {
        label: '业务类型',
        data: 'business'
      },
      {
        label: '服务类型',
        data: 'service'
      },
      {
        label: '服务覆盖区县',
        data: 'district'
      }
    ]
    return {
      businessArr: [],
      serviceArr: [],
      isOpen: false,
      visible: false,
      searchLoading: false,
      search: {
        logAccount: '',
        workerName: '',
        phone: '',
        businessNo: '',
        serviceNo: '',
        address: []
      },
      columns,
      tabs: []
    }
  },
  created() {
    getInstallBusinessType().then(arr => {
      this.businessArr = arr
    })
    getInstallServiceType().then(arr => {
      this.serviceArr = arr
    })
  },
  methods: {
    // 搜索
    searchFunc(search) {
      const basicTable = this.$refs.BasicTable
      if (basicTable) {
        let params = copy(search)
        params.provinceNo = dotData(params, 'addressArr[0]') || ''
        params.cityNo = dotData(params, 'addressArr[1]') || ''
        params.districtNo = dotData(params, 'addressArr[2]') || ''
        delete params.addressArr
        this.searchLoading = true
        basicTable.search(params).finally(_ => {
          this.searchLoading = false
        })
      }
    },
    // 重置表单
    resetFields() {
      const searchForm = this.$refs.searchForm
      if (searchForm) {
        searchForm.resetFields()
      }
    },
    // 表格刷新
    refresh() {
      this.searchFunc(this.search)
    },
    add() {
      this.$refs.editModal.show()
    },
    editRowId(data) {
      this.$refs.editModal.show(data)
    },
    // 删除
    delRowId(data) {
      confirm('你确定要从安装资源调度里移除该师傅信息吗？', '温馨提示', {
        confirmButtonLoading: true,
        type: 'success',
        confirmCallBack: ({confirmButtonLoadingClose, close}) => {
          console.log('confirmCallBack')
          return this.$ajax.post('/ms-warehouse-order/resourceManage/operateInstall', {
            operation: 'delete',
            id: data.id
          }).then(response => {
            close()
            if (response.success) {
              this.refresh()
              this.$message({
                type: 'success',
                message: '移除成功!'
              })
            }
          }).finally(_ => {
            confirmButtonLoadingClose()
          })
        }
      })
    }
  }
}
</script>
